package priv.pattern.builder;

// 建造者模式用于链式配置对象，最后再进行构建

public class BuilderPattern {
    public static void main(String[] args) {
        Student student = Student.builder().id(1).age(18).name("Tom").gender("Male").build();
        System.out.println(student.toString());
    }
}

class Student {
    private int id;
    private String name;
    private int age;
    private String gender;
    // 私有化构造方法
    private Student(int id, String name, int age, String gender) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private int id;
        private String name;
        private int age;
        private String gender;

        public Builder id(int id) {
            this.id = id;
            return this;
        }

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder age(int age) {
            this.age = age;
            return this;
        }

        public Builder gender(String gender) {
            this.gender = gender;
            return this;
        }

        // 通过build()方法创建对象
        public Student build() {
            return new Student(id, name, age, gender);
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }
}
